package com.heimali.happyreader.http;

import java.io.IOException;
import java.net.URI;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.text.TextUtils;

import com.heimali.happyreader.debug.Console;
import com.heimali.happyreader.utils.SystemUtils;

public class Request {
	private static final String DEFAULT_CHARSET = "GBK";
	private static final String HEADER_PARAM_HOST = "HOST";
	
	static public String get(URI uri) {
		return get(uri, DEFAULT_CHARSET);
	}
	
	/**
	 * 绑定host的get请求
	 * @param uri
	 * @param hostname 
	 * @return 
	 */
	static public String getWithHost(URI uri, String hostname) {
		return get(uri, null, hostname);
	}
	
	static public String get(URI uri, String charset) {
		return get(uri, charset, null);
	}
	
	/**
	 * get请求
	 * @param uri ： 如 http://173.83.127.172/index.html  此IP必须是服务器IP，不能是LVS的IP
	 * @param charset : 页面编码， 默认为GBK
	 * @param hostname : 要访问的域名 www.heimali.com
	 * @return 请求体内容
	 */
	static public String get(URI uri, String charset, String hostname) {
		String strResult = "request error";
		HttpGet request = new HttpGet(uri);
		if (TextUtils.isEmpty(charset)) {
			charset = DEFAULT_CHARSET;
		}
		if (!TextUtils.isEmpty(hostname)) {
			request.addHeader(HEADER_PARAM_HOST, hostname);
		}
		
		try {
			HttpClient client = new DefaultHttpClient();
			Console.log(request.getURI());
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				strResult = EntityUtils.toString(response.getEntity(), charset);
			}
		}catch(ClientProtocolException e) {
			SystemUtils.Log("ClientProtocolException", e.getMessage());
			e.getStackTrace();
		}catch(IOException e) {
			SystemUtils.Log("IOException", e.getMessage());
			e.getStackTrace();
		}
		return strResult ;
	}
	
	static public String postWithHost(URI uri, String hostname) {
		return post(uri, null, hostname);
	}
	
	static public String post(URI uri, String charset) {
		return post(uri, charset, null);
	}
	
	static public String post(URI uri, String charset, String hostname) {
		String strResult = "request error";
		HttpPost request = new HttpPost(uri);
		if (TextUtils.isEmpty(charset)) {
			charset = DEFAULT_CHARSET;
		}
		if (!TextUtils.isEmpty(hostname)) {
			request.addHeader(HEADER_PARAM_HOST, hostname);
		}
		
		try {
			HttpClient client = new DefaultHttpClient();
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				strResult = EntityUtils.toString(response.getEntity(), charset);
			}
		}catch (ClientProtocolException e) {
			System.out.println("ClientProtocolException "+e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("IOException "+e.getMessage());
			e.printStackTrace();
		}
		
		return strResult;
	}
	
}
